var ws, arrId, nums, price, zong, total, title, pic, hid, cart;
var uid;
document.addEventListener('plusready', function(){
	uid = plus.storage.getItem('uid');
	ws = plus.webview.currentWebview();
	spage = ws.spage;
	arrId = ws.arrId;
	nums = ws.nums;
	price = ws.price;
	zong = ws.zong;
	total = ws.total;
	title = ws.title;
	pic = ws.pic;
	hid = ws.hid;	//医馆id
	cart = ws.cart;		//购物车id
	console.log(arrId+" "+nums+" "+price+" "+zong+" "+total+" "+title+" "+pic+" "+hid+" "+cart);
	$('.total').text('共计商品：'+total);
	$('.zong').text('¥'+zong);
	pid  = arrId.split(',');
	num  = nums.split(',');
	pric  = price.split(',');
	titl  = title.split(';');
	pict  = pic.split(';');
	var list = '';
	for(var i = 0; i < pid.length - 1; i++){
		list += '<ul class="mui-table-view" style="margin-top: 10px;">'+
			'<li class="mui-table-view-cell mui-media">'+
				'<a href="javascript:;">'+
					'<img class="mui-media-object mui-pull-left" src="'+getAvatar(pict[i])+'">'+
					'<div class="mui-media-body">'+titl[i]+
						'<p class="mui-ellipsis">¥'+pric[i]+'/g<span class="mui-pull-right rightnub">×'+num[i]+'</span></p>'+
					'</div>'+
				'</a>'+
			'</li>'+
		'</ul>';
	}
	$('.list').append(list);
	//加载运费和收货地址
	console.log(apiRoot+"/home/medicinal/doseRule/aid/8/uid/"+uid);
	$.ajax({
		type:"get",
		url:apiRoot+"/home/medicinal/doseRule/aid/8/uid/"+uid, 
		dataType :'json',
		success :function(data){
			console.log(JSON.stringify(data)); 
			if(data.fare)$('.freight').text(data.fare);
			if(data.name)$('.name').text(data.name);
			if(data.phone)$('.phone').text(data.phone);
			if(data.address)$('.address').text(data.address+" "+data.info);
			if(data.zipcode)$('.zipcode').val(data.zipcode);	//邮编
		},
		error :function(e){
			console.log(JSON.stringify(e));
			errortoast();
		}
	});
	//点击提交订单
	$('button').on('tap', function(){
		var name = $('.name').text();
		var phone = $('.phone').text();
		var address = $('.address').text();
		var zipcode = $('.zipcode').val();
		var freight = $('.freight').text();
		if(!name.trim() || !phone.trim() || !address.trim() || !zipcode.trim()){
			toast('请填写收货地址');return;
		}
//console.log(apiRoot+"/home/medicinal/addOrder/uid/"+uid+"/name/"+name+"/phone/"+phone+"/address/"+address+"/zipcode/"+zipcode+"/arrid/"+arrId+"/num/"+nums+"/price/"+price+"/hid/"+hid+"/cart/"+cart)
//		return;
		$.ajax({
			type :"post",
			url :apiRoot+"/home/medicinal/addOrder",
			data :{
				uid :uid,
				name :name,
				phone :phone,
				address :address,
				zipcode :zipcode,
				arrid :arrId,
				num :nums, 
				price :price, 
				fare :freight,
				hid :hid,
				cart :cart
			},
			success :function(data){
				console.log(JSON.stringify(data));
				if(data > 0){
					toast('提交成功');
					ws.close();
					if(spage)plus.webview.getWebviewById(spage).close();
				}
			},
			error :function(e){
				console.log(JSON.stringify(e));
				errortoast();
			}
		});
	})
	//点击进入选择收货地址
	$('#content ul').eq(0).on('tap', function(){
		goNewPage('shop-shouhuo.html',{spage:ws.id})
	})
}, false)
//编辑收货地址
window.addEventListener('changeaddress', function(e){
	var name = e.detail.name;
	var phone = e.detail.phone;
	var address = e.detail.address;
	var info = e.detail.info;
	var zipcode = e.detail.zipcode;
	if(name)$('.name').text(name);
	if(phone)$('.phone').text(phone);
	if(zipcode)$('.zipcode').val(zipcode);
	if(address)$('.address').text(address+" "+info);
}, false)